package com.aigou.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class AiGouUtil {

	@Autowired
    StringRedisTemplate stringRedisTemplate;
	
	/**
	 * 创建订单号
	 * @param pre
	 * @return
	 */
	public String createOrderNum(String pre) {
		java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		
		String num = pre + sdf.format(new Date());
		Boolean boolean1 = stringRedisTemplate.opsForValue().setIfAbsent(num, "11", 2, TimeUnit.SECONDS);
		if (boolean1) {
			return num;
		}
		
		return createOrderNum(pre);
	}
	
}
